﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Homebrew_SD_Manager
{
    public partial class Roms : Form
    {
        public Roms()
        {
            InitializeComponent();
        }

        private void Roms_Load(object sender, EventArgs e)
        {
            init_emul();
            init_back(Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor);
            init_fore(Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor);
        }

        private void init_emul()
        {
            DirectoryInfo dInfo = new DirectoryInfo(Environment.CurrentDirectory);
            foreach (DirectoryInfo di in dInfo.GetDirectories())
            {
                if (di.Name == "snes9xgx" || di.Name == "vbagx")
                    cbEmul.Items.Add(di.Name);
            }
            if (cbEmul.Items.Count == 0)
            {
                MessageBox.Show("Aucun dossier d'émulateur trouvé !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            else
                cbEmul.Text = cbEmul.Items[0].ToString();
        }

        private void init_roms(string emul)
        {
            try
            {
                lvRoms.Items.Clear();
                progressBar1.Value = 0;
                if (emul.Substring(emul.Length - 2, 2) == "gx")
                {
                    DirectoryInfo dInfo = new DirectoryInfo(emul + @"\roms");
                    progressBar1.Maximum = dInfo.GetFiles().Count();
                    foreach (FileInfo fi in dInfo.GetFiles())
                    {
                        lvRoms.Items.Add(fi.Name);
                        progressBar1.Value++;
                    }
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }

        private void init_back(int color)
        {
            if (color != 0)
            {
                BackColor = Color.FromArgb(color);
                cbEmul.BackColor = Color.FromArgb(color);
                lvRoms.BackColor = Color.FromArgb(color);
                btAdd.BackColor = Color.FromArgb(color);
                btDelete.BackColor = Color.FromArgb(color);
            }
        }

        private void init_fore(int color)
        {
            if (color != 0)
            {
                ForeColor = Color.FromArgb(color);
                cbEmul.ForeColor = Color.FromArgb(color);
                lvRoms.ForeColor = Color.FromArgb(color);
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                progressBar1.Value = 0;
                progressBar1.Maximum = openFileDialog1.FileNames.Count();
                foreach (string file in openFileDialog1.FileNames)
                {
                    File.Copy(file, cbEmul.Text + @"\roms\" + Path.GetFileName(file), true);
                    progressBar1.Value++;
                }
                cbSelectAll.Checked = false;
                init_roms(cbEmul.Text);
            }
        }

        private void lvRoms_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (lvRoms.CheckedItems.Count != 0)
                btDelete.Enabled = true;
            else
                btDelete.Enabled = false;
        }

        private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelectAll.Checked == true)
            {
                for (int i = 0; i < lvRoms.Items.Count; i++)
                    lvRoms.Items[i].Checked = true;
            }
            else
            {
                for (int i = 0; i < lvRoms.Items.Count; i++)
                    lvRoms.Items[i].Checked = false;
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            progressBar1.Maximum = lvRoms.CheckedItems.Count;
            for (int i = 0; i < lvRoms.CheckedItems.Count; i++)
            {
                File.Delete(cbEmul.Text + @"\roms\" + lvRoms.CheckedItems[i].Text);
                progressBar1.Value++;
            }
            cbSelectAll.Checked = false;
            init_roms(cbEmul.Text);
        }

        private void cbEmul_SelectedIndexChanged(object sender, EventArgs e)
        {
            init_roms(cbEmul.Text);
        }
    }
}
